<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            报废
        </div>
    </header>
    <!-- <div class="aui-content aui-margin-b-15" style="margin-top:50px;"> -->
    <!-- </br> -->
    <!-- //radio -->
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">模式 </div>
                    <div class="aui-list-item-input">
                        <label><input class="aui-radio" type="radio" value="1" name="type" checked>产品条码</label>
                        <label><input class="aui-radio" type="radio" value="2" name="type"> 零件码</label>
                    </div>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描/输入 条码" id="consignment" value=""
                            onclick="getIdSelector();">
                    </div>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">缺陷类别</div>
                    <div class="aui-list-item-input">
                        <select id="qualchkitemType">
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">缺陷内容</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择缺陷内容" id="qualchkitem" value="" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectqualchkitem()"></i>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">班&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp次</div>
                    <div class="aui-list-item-input">
                        <select id="shiftCode">
                        </select>
                    </div>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">备注</div>
                    <div class="aui-list-item-input">
                        <input id="remark"></input>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">蓝牙设备</div>
                    <select id="bluetooth">
                    </select>
                </div>
            </li>
        </ul>
    </div>
    <!-- <div class="aui-btn aui-btn-info aui-btn-block" style="width:60%;margin-left:20%;font-size: 12px;font-weight: bold;" onclick="createUqcmChkBill()">一键确认</div> -->
    <div class=' aui-btn aui-btn-info aui-btn-block' onclick='replace()'>保存
    </div>
    <div style="width:100%;margin-top:2%;"></div>
    <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div>

    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id

    var qualchkitemCode = "";
    var qualchkitemName = "";
    var qualchkitemGid = "";

    //初始化查询仓库下拉框
    apiready = function () {
        //获取蓝牙列表
        getBluetoothCombox();
        $("#consignment").select();
        // $("#consignment").focus();
        // idSelector = "consignment";

        //2:查询班次下拉框
        doSearchTeamShift();
        //查询缺陷类别下拉框
        doSearchUqcqualchkitem();

    }

      //产线选择后回调函数
      function setQualchkitem(name) {
        qualchkitemCode = name;
        qualchkitemName = name;
        document.getElementById('qualchkitem').value = name;
    }

    //班组下拉框
    function doSearchTeamShift() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyTeam";
        var data = {
            values: {
                workCenterCode: '',
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    //班次赋值
                    var listShift = eval(myobj.listShift);
                    var htmlShift = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < listShift.length; i++) {
                        htmlShift += "<option value='" + listShift[i].code + "'>" + listShift[i].name + "</option>";
                    }
                    $api.html($api.byId("shiftCode"), htmlShift);
                    $("#shiftCode").val(api.pageParam.shiftCode);

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }




    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.connectBluetooth({
            address: address,
        }, function (ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }


    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.searchBluetooth({}, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }



    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：二维码
            if (idSelector == 'consignment') {

                //二维码
                $("#consignment").val(ret);

                //查询表格数据
                scanQrCode();

            }
        });
    }

    //发货单监听回车事件
    $("#consignment").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var consignment = $(this).val();
        if (keycode == '13') {
            $("#consignment").blur();
            //发货单
            $("#consignment").val(consignment);

            //查询表格数据
            scanQrCode();
        }

    });

    function scanQrCode() {
        $("#remark").select();
        idSelector = "remark";
    }

    function replace() {
        var barCode = $("#consignment").val();
        var type = $("input[name='type']:checked").val();
        var types = $("#qualchkitemType").val();

        if (types == null || types == "") {

            api.toast({
                msg: "请先选择缺陷类别！",
                duration: 3000
            });
            return
        }

        var qualchkitem = document.getElementById('qualchkitem').value;

        if (qualchkitem == null || qualchkitem == "") {

            api.toast({
                msg: "请先选择缺陷内容！",
                duration: 3000
            });
            return
        }
        var shiftCode = $("#shiftCode").val();
        if (shiftCode == null || shiftCode == "" || shiftCode == undefined) {
            api.toast({
                msg: "请选择班次!",
                duration: 3000
            });
            return;
        }
        var remark = $("#remark").val();

        var address = $("#bluetooth").val();

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }

        localStorage.setItem('bluetooth', address);
        api.confirm({
            title: '确认报废',
            msg: "是否确认报废该条码：" + barCode,
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                console.log('点击了确定按钮');
                api.showProgress({
                    title: '保存中...',
                });
                var un = localStorage.getItem('un');
                var serverUrl = localStorage.getItem('serverUrl');
                var url = serverUrl + "/app/scarpBarcode";
                var data = {
                    values: { ////以表单方式提交参数（JSON对象）
                        un: un,
                        barCode: barCode,
                        type: type,
                        qualchkitemType: types,
                        qualchkitem: qualchkitem,
                        qualchkitemCode: qualchkitemCode,
                        shiftCode: shiftCode,
                        remark: remark,
                    }
                };

                api.ajax({
                    url: url,
                    method: 'post',
                    dataType: 'json',
                    timeout: 30,
                    returnAll: false,
                    data: data
                }, function (ret, err) {
                    if (ret) {
                        api.hideProgress();
                        var data = JSON.stringify(ret);
                        var myobj = eval("(" + data + ")");
                        //返回成功
                        if (myobj.code == "0") {
                            document.getElementById("consignment").value = "";
                            $("#consignment").select();
                            $("#qualchkitemType").val("");

                            document.getElementById('qualchkitem').value = "";

                            document.getElementById('remark').value = "";
                            api.toast({
                                msg: myobj.msg,
                                duration: 3000
                            });
                            var json = myobj.json;
                            //调用PDA打印
                            printPDA(json, address);
                            //失败
                        } else if (myobj.code == "1") {

                            document.getElementById("consignment").value = "";
                            $("#consignment").select();
                            idSelector = "consignment";
                            api.toast({
                                msg: myobj.msg,
                                duration: 3000
                            });
                        }
                    } else {
                        api.hideProgress();
                    }
                });
            } else {
                $("#consignment").val("");
            }
        });
    }
    //打印
    function printPDA(json, address) {
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.printerXc({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //缺陷内容下拉选
    function goSelectqualchkitem() {
        var type = $("#qualchkitemType").val();
        if (type) {
            var content = $("#qualchkitem").val();
            api.openWin({
                name: 'usmSelectQualchkitem',
                url: 'usmSelectQualchkitem.html',
                pageParam: {
                    code: type,
                    name: "scrap",
                    content: content
                }
            });

        } else {
            api.toast({
                msg: "请先选择缺陷类别！",
                duration: 3000
            });
        }

    }


    //查询缺陷类别下拉框
    function doSearchUqcqualchkitem() {
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                tablename: "qc_bd_chk_item_type",
                column_name: "name",
                column_value: "code",
                // condition: " ext01 = 'LLJC'",
                un: 'combox',
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            async: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);

                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("qualchkitemType"), html);
            }

        });
    }




    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }




    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>